﻿using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using NewRetail.Application.Components.Platform;
using NewRetail.Application.Components.Platform.Entity;
using NewRetail.Application.Core;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Volo.Abp.Application.Dtos;

namespace NewRetail.Api.Controllers
{
    [NewRetailRoute("pos-pay-flow")]
    public class PosPayFlowController : BaseController<PosPayFlow, PosSalesPayFlowFilterDto>
    {
        private readonly IPosSalesPayFlowService posSalesPayFlowService;
        public PosPayFlowController(
            IPosSalesPayFlowService posSalesPayFlowService)
        {
            this.posSalesPayFlowService = posSalesPayFlowService;
        }

        protected override async Task<IQueryable<PosPayFlow>> FilterQueryPaging(DbContext db, IQueryable<PosPayFlow> query, PosSalesPayFlowFilterDto dto)
        {
            if (!string.IsNullOrWhiteSpace(dto.warehouseId))
            {
                query = query.Where(x => x.warehouseID == dto.warehouseId);
            }
            if (!string.IsNullOrWhiteSpace(dto.cashierId))
            {
                query = query.Where(x => x.cashierID == dto.cashierId);
            }
            if (!string.IsNullOrWhiteSpace(dto.flowNo))
            {
                query = query.Where(x => x.flowNo.Contains(dto.flowNo));
            }

            if (!string.IsNullOrWhiteSpace(dto.cashierName))
            {
                var cashierIds = db.Set<PosCashier>().AsNoTracking()
                    .Where(x => x.cashierName.Contains(dto.cashierName)).Select(x => x.Id);

                query = query.Where(x => cashierIds.Contains(x.cashierID));
            }

            if (dto.startTime != null)
            {
                query = query.Where(x => x.payTime >= dto.startTime.Value);
            }
            if (dto.endTime != null)
            {
                query = query.Where(x => x.payTime < dto.endTime.Value);
            }
            ////过滤掉找零和自动抹零
            //var payWayFilter = new List<string> { "X", "AD" };
            //query = query.Where(x => !payWayFilter.Contains(x.payWay));

            if (!string.IsNullOrWhiteSpace(dto.payWay))
            {
                query = query.Where(x => x.payWay == dto.payWay);
            }

            return await base.FilterQueryPaging(db, query, dto);
        }

        protected override async Task<IQueryable<PosPayFlow>> OrderQueryPaging(IQueryable<PosPayFlow> query, PosSalesPayFlowFilterDto dto)
        {
            await Task.CompletedTask;

            query = query.OrderByDescending(x => x.payTime);
            return query;
        }

        public override async Task<ResponseEntity<PagedResultDto<PosPayFlow>>> QueryPaging([FromBody] PosSalesPayFlowFilterDto dto)
        {
            var res = await base.QueryPaging(dto);

            if (res.Data?.Items?.Any() ?? false)
            {
                res.Data.Items = await this.posSalesPayFlowService.AttachCashier(res.Data.Items.ToArray());
            }

            return res;
        }

        #region 订单流水支付显示 手机端用，剔除现金剩余其他方式
        [HttpPost("query-pospayflowlist")]
        public async Task<ResponseEntity<PagedResultDto<PosPayFlow>>> QueryPosPayFlowSalesList([FromBody] PosSalesPayFlowFilterDto inputDto)
        {
            var ret = new ResponseEntity<PagedResultDto<PosPayFlow>>();
            try
            {
                var result = await this.posSalesPayFlowService.QueryPosPayFlowSales(inputDto);
                ret.SetData(result);
            }
            catch (Exception e)
            {
                ret.SetError("失败！" + e.Message);
            }

            return ret;
        }
        #endregion

        #region 订单流水支付显示 后端用
        [HttpPost("query-allpospayflowlist")]
        public async Task<ResponseEntity<PagedResultDto<PosPayFlow>>> QueryAllPosPayFlowSalesList([FromBody] PosSalesPayFlowFilterDto inputDto)
        {
            var ret = new ResponseEntity<PagedResultDto<PosPayFlow>>();
            try
            {
                var result = await this.posSalesPayFlowService.QueryPosPayFlowSales(inputDto, true);
                ret.SetData(result);
            }
            catch (Exception e)
            {
                ret.SetError("失败！" + e.Message);
            }

            return ret;
        }
        #endregion
    }
}
